package com.cby.operation.sdk.storage

import android.content.Context
import com.cby.operation.common.encrypt.Base64Utils
import com.cby.operation.sdk.SdkConfiguration.gameId

/**
 * Description:存储配置信息
 *
 * @author 陈宝阳
 * @create 2020/8/14 10: 40
 */
object SDKStorage {
    /**
     * 该文件同步付费的同一个文件名（为了在接入付费下载和内购SDK，登录授权只要一次）
     */
    private const val FILE_NAME = "hykb_login_info"
    private const val KEY_VALIDITY = "validity"
    private const val KEY_FIRST_TIME = "timestamp"
    private const val KEY_USER_STATE = "state"
    private const val KEY_USER_NICK = "nick"
    private const val KEY_USER_ACCESS_TOKEN = "accessToken"

    /**
     * 这个字段文件，表示是否来自付费下载
     */
    private const val KEY_FROM_PAID = "from_paid"
    private val fileName: String
        private get() = FILE_NAME + gameId

    /**
     * 保存用户的uid|token|type 数据串密码串
     *
     * @param context
     * @param value  [UserUtils.getState]
     * @return
     */
    fun saveUserState(context: Context, value: String?): Boolean {
        val settings = context.getSharedPreferences(fileName, Context.MODE_PRIVATE)
        val editor = settings.edit()
        editor.putString(KEY_USER_STATE, value)
        return editor.commit()
    }

    /**
     * 保存用户的uid|token|type 数据串密码串
     *
     * @param context
     * @return
     */
    fun getUserState(context: Context): String? {
        val settings = context.getSharedPreferences(fileName, Context.MODE_PRIVATE)
        val value = settings.getString(KEY_USER_STATE, "")
        return Base64Utils.decode(value)
    }

    /**
     * 保存用户的昵称
     *
     * @param context
     * @param value  [HykbUser.getNick]
     * @return
     */
    fun saveUserNick(context: Context, value: String?): Boolean {
        val settings = context.getSharedPreferences(fileName, Context.MODE_PRIVATE)
        val editor = settings.edit()
        editor.putString(KEY_USER_NICK, value)
        return editor.commit()
    }

    /**
     * 获取用户的昵称
     *
     * @param context
     * @return
     */
    fun getUserNick(context: Context): String? {
        val settings = context.getSharedPreferences(fileName, Context.MODE_PRIVATE)
        return settings.getString(KEY_USER_NICK, "")
    }

    /**
     * 保存用户的对外校验Token
     *
     * @param context
     * @param value
     * @return
     */
    fun saveUserAccessToken(context: Context, value: String?): Boolean {
        val settings = context.getSharedPreferences(fileName, Context.MODE_PRIVATE)
        val editor = settings.edit()
        editor.putString(KEY_USER_ACCESS_TOKEN, value)
        return editor.commit()
    }

    /**
     * 获取用户的对外校验Token
     *
     * @param context
     * @return
     */
    fun getUserAccessToken(context: Context): String? {
        val settings = context.getSharedPreferences(fileName, Context.MODE_PRIVATE)
        return settings.getString(KEY_USER_ACCESS_TOKEN, "")
    }

    /**
     * 保存用户有效的天数
     *
     * @param context
     * @param value
     * @return
     */
    fun saveValidity(context: Context, value: Int): Boolean {
        val settings = context.getSharedPreferences(fileName, Context.MODE_PRIVATE)
        val editor = settings.edit()
        editor.putString(KEY_VALIDITY, Base64Utils.encode(value.toString()))
        return editor.commit()
    }

    /**
     * 获取用户有效的天数
     *
     * @param context
     * @return
     */
    fun getValidity(context: Context): Int {
        val settings = context.getSharedPreferences(fileName, Context.MODE_PRIVATE)
        val value = settings.getString(KEY_VALIDITY, "")
        return Base64Utils.decode(value)!!.toInt()
    }

    /**
     * 保存截止日期
     *
     * @param context
     * @param value
     * @return
     */
    fun saveFirstTime(context: Context, value: Long): Boolean {
        val settings = context.getSharedPreferences(fileName, Context.MODE_PRIVATE)
        val editor = settings.edit()
        editor.putLong(KEY_FIRST_TIME, value)
        return editor.commit()
    }

    /**
     * 获取截止日期
     *
     * @param context
     * @return
     */
    fun getFirstTime(context: Context): Long {
        val settings = context.getSharedPreferences(fileName, Context.MODE_PRIVATE)
        return settings.getLong(KEY_FIRST_TIME, 0L)
    }

    /**
     * 获取用户信息来自付费下载
     *
     * @param context
     * @return
     */
    fun getFlag(context: Context): Boolean {
        val settings = context.getSharedPreferences(fileName, Context.MODE_PRIVATE)
        return settings.getBoolean(KEY_FROM_PAID, false)
    }

    /**
     * 清除掉本地缓存
     * @param context
     */
    fun clearCache(context: Context) {
        saveValidity(context, 0)
        saveFirstTime(context, 0L)
        saveUserState(context, "")
        saveUserNick(context, "")
    }
}